package com.example.io.datajek.springbasics.movierecommendersystem.lesson9;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

@SpringBootApplication
@ComponentScan(basePackages = "com.example.io.datajek.springbasics.movierecommendersystem.lesson10")
@ComponentScan(includeFilters = @ComponentScan.Filter (type= FilterType.REGEX, pattern="io.datajek.spring.basics.movierecommendersystem.lesson9.*"))
public class MovieRecommenderSystemApplication {
    public static void main(String[] args){
        //ApplicationContext负责管理Bean和依赖关系。
        //使用ApplicationContext来查找正在使用的过滤器
        ApplicationContext appContext = SpringApplication.run(MovieRecommenderSystemApplication.class, args);
        System.out.println("ContentBasedFilter bean found = " + appContext.containsBean("contentBasedFilter"));
        System.out.println("CollaborativeFilter bean found = " + appContext.containsBean("collaborativeFilter"));
        //从Application Context中获得Singleton Bean
//        ContentBasedFilter filter = appContext.getBean(ContentBasedFilter.class);
//        System.out.println("\nContentBasedFilter bean with singleton scope");
//        System.out.println(filter);
//
//        //从Singleton Bean中获得Prototype Bean三次
//        Movie movie1 = filter.getMovie();
//        Movie movie2 = filter.getMovie();
//        Movie movie3 = filter.getMovie();
//
//        System.out.println("\nMovie bean with prototype scope");
//        System.out.println(movie1);
//        System.out.println(movie2);
//        System.out.println(movie3);
//
//        //打印各种Bean的实例数
//        System.out.println("\nContentBasedFilter instances created: "+
//                ContentBasedFilter.getInstances());
//        System.out.println("Movie instances created: "+ Movie.getInstances());
        //从Application Context中三次检索Singleton Bean
//        CollaborativeFilter nb = appContext.getBean(CollaborativeFilter.class);
//        CollaborativeFilter nb555 = appContext.getBean(CollaborativeFilter.class);
//        CollaborativeFilter nb666 = appContext.getBean(CollaborativeFilter.class);
//
//        System.out.println(444);
//        System.out.println(555);
//        System.out.println(666);
        //System.out.println("Calling getBean() on RecommenderImplementation");
        //调用getBean方法来显示构造函数注入的发生
        //RecommenderImplementation recommender = appContext.getBean(RecommenderImplementation.class);
        //调用方法来获得推荐
        //String[] result = recommender.recommendMovies("Finding Dory");
        //显示结果
        //System.out.println(Arrays.toString(result));

//        RecommenderImplementation2使用setter方法注入依赖关系
//        System.out.println("Calling getBean() on RecommenderImplementation2");
//        RecommenderImplementation2 recommender2 = appContext.getBean(RecommenderImplementation2.class);
//        String[] result = recommender2.recommendMovies("Finding Dory");
//        System.out.println(Arrays.toString(result));
    }
}
